package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.pojo.*;
import com.ruoyi.service.*;
import com.ruoyi.utils.LoansUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/loans")
public class LoansController {

    @Resource
    private AppService appService;

    @Autowired
    private LoansService loansService;

    @Autowired
    private ProductService productService;

    @Autowired
    private RefundService refundService;

    @Autowired
    private UesrService uesrService;

    @Autowired
    private ContractService contractService;

    //等额本金，等额本息计算还款总额及每月还款金额
    @PostMapping("/repayment")
    public Map<String,Object> repayment(@RequestBody Loans loans){
        //获取还款方式
        String loanstype = loans.getLoanstype();
        //获取本金
        Integer principal = loans.getPrincipal();
        //计算等额本金
        if (loanstype.equals("等额本金")){
            return LoansUtils.capital(principal,loans);
        }
        //计算等额本息
        if (loanstype.equals("等额本息")) {
            return LoansUtils.interests(principal,loans);
        }
        return null;
    }

    //生成贷款
    @PostMapping("/loansadd")
    public Loans loansadd(@RequestBody Loans loans){
        //获取用户id
//        String token = response.getHeader("token");
//        Integer tokenUid = appService.tokenUserId(token);
        Integer uid = 1;
        //查询贷款产品利率
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("pid",loans.getPid());
        List<Product> productList = productService.list(productQueryWrapper);
        Product product = productList.get(0);
        loans.setInterestrate(product.getInterestrate());
        //获取还款期数
        QueryWrapper<Refund> refundQueryWrapper = new QueryWrapper<>();
        refundQueryWrapper.eq("rid",loans.getRid());
        List<Refund> refundList = refundService.list(refundQueryWrapper);
        Refund refund = refundList.get(0);
        loans.setNumbe(refund.getRnumbe());
        //计算应还款金额
        Double money = 0.0;
        //获取还款方式
        String loanstype = loans.getLoanstype();
        Integer principal = loans.getPrincipal();
        //计算等额本金
        if (loanstype.equals("等额本金")){
            Map<String, Object> capital = LoansUtils.capital(principal, loans);
            money = (Double) capital.get("sum");
        }
        //计算等额本息
        if (loanstype.equals("等额本息")) {
            Map<String, Object> interests = LoansUtils.interests(principal, loans);
            money = (Double) interests.get("sum");
        }
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MONTH,refund.getRnumbe());
        loans.setBorrowtime(date);
        loans.setReturntime(calendar.getTime());
        loans.setRtype("正在还款");
        loans.setWithdraw("未提现");
        loans.setUid(uid);
        Integer intMoney = (int)(money * 100);
        loans.setMoney((long)intMoney);
        loansService.save(loans);
        return loans;
    }

    //生成合同
    @PostMapping("/contractadd")
    public void contractAdd(@RequestBody Contract contract){
        //获取用户id
//        String token = response.getHeader("token");
//        Integer tokenUid = appService.tokenUserId(token);
        Integer uid = 1;
        //查询贷款用户信息
        QueryWrapper<Uesr> uesrQueryWrapper = new QueryWrapper<>();
        uesrQueryWrapper.eq("uid",uid);
        List<Uesr> uesrList = uesrService.list(uesrQueryWrapper);
        Uesr uesr = uesrList.get(0);
        //查询贷款表数据
        QueryWrapper<Loans> loansQueryWrapper = new QueryWrapper<>();
        loansQueryWrapper.eq("lid",contract.getLid());
        List<Loans> loansList = loansService.list();
        Loans loans = loansList.get(0);
        contract.setFirstparty("放款方");
        contract.setFirstPartyIdentityCard("000000000000000000");
        contract.setSecondparty(uesr.getUname());
        contract.setSecondPartyIdentityCard(uesr.getIdentitycard());
        contract.setMoney((long)loans.getPrincipal());
        contract.setRid(loans.getRid());
        contract.setInterestrate(loans.getInterestrate());
        contract.setBorrowtime(loans.getBorrowtime());
        contract.setReturnduration(loans.getReturntime());
        contract.setInallmoney(loans.getMoney());
        contractService.save(contract);
    }
}
